package com.renwei.yunlong.base;

import android.app.Activity;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.annotation.UiThread;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.renwei.yunlong.bean.LoginBean;
import com.renwei.yunlong.bean.ServiceLoginBean;
import com.renwei.yunlong.utils.ACache;
import com.renwei.yunlong.utils.LogUtil;
import com.renwei.yunlong.utils.StringUtils;

import butterknife.ButterKnife;
import github.opensource.dialog.BeToastUtil;

/**
 * Created by wangqi on 2017/9/30.
 */

public abstract class BaseLazyFragment extends Fragment {
    /**
     * 懒加载过
     */
    private boolean isLazyLoaded;

    private boolean isPrepared;
    protected BaseActivity mActivity;
    private View view;

    protected ACache mCache;
    protected ServiceLoginBean serviceLoginBean;
    protected LoginBean ownerBean;
    protected String companyType;

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        this.mActivity = (BaseActivity) activity;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        if (view == null) {
            view = inflater.inflate(getLayoutId(), container, false);
            mCache = ACache.get(getContext());
            companyType = mCache.getAsString("companyType");
            ownerBean = (LoginBean) mCache.getAsObject("loginBean");
            serviceLoginBean = (ServiceLoginBean) mCache.getAsObject("serviceLoginBean");
            ButterKnife.bind(this, view);
        }
        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        isPrepared = true;
        //只有Fragment onCreateView好了，
        //另外这里调用一次lazyLoad(）
        lazyLoad();
    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        lazyLoad();
    }

    public void refreshMcache() {
        companyType = mCache.getAsString("companyType");
        ownerBean = (LoginBean) mCache.getAsObject("loginBean");
        serviceLoginBean = (ServiceLoginBean) mCache.getAsObject("serviceLoginBean");
    }

    public String getCompanyCode() {
        String companyCode = "";
        if ("1".equals(companyType)) {
            companyCode = StringUtils.value(ownerBean.getRows().getCompany().getCompanyCode());
        } else if ("2".equals(companyType)) {
            companyCode = StringUtils.value(serviceLoginBean.getRows().getCompany().getCompanyCode());
        } else {
            LogUtil.e("公司数据错误");
            return "";
        }
        return companyCode;
    }

    //显示信息toast
    protected void showCenterInfoMsg(String text) {
        if (!TextUtils.isEmpty(text)) {
            BeToastUtil.get().showCenterInfoMsg(getActivity(), text);
        }
    }


    //显示成功toast
    protected void showCenterSuccessMsg(String text) {
        if (!TextUtils.isEmpty(text)) {
            BeToastUtil.get().showCenterSuccessMsg(getActivity(), text);
        }
    }

    //显示错误snaker
    protected void showTopWrongMsg(String text) {
        if (!TextUtils.isEmpty(text)) {
            BeToastUtil.get().showTopWrongMsg(getActivity(), text);
        }
    }


    /**
     * 调用懒加载
     */

    private void lazyLoad() {
        if (getUserVisibleHint() && isPrepared && !isLazyLoaded) {
            onLazyLoad();
            isLazyLoaded = true;
        }
    }

    protected abstract int getLayoutId();

    @UiThread
    public abstract void onLazyLoad();
}
